#include "bsp_uart.h"
uint8_t GET_DATA=0;
uint8_t LVGL_Angle_Value[2];

volatile uint8_t receive_state = 0;      // ???????


// ???????
#define STATE_WAIT_FF1      0  // ?????0xFF
#define STATE_WAIT_FF2      1  // ?????????(0xFF?0xFE)
#define STATE_WAIT_DATA1    2  // ????1??
#define STATE_WAIT_DATA2    3  // ????2??

void USART1_IRQHandler(void)
{
  if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  {
        uint8_t received_byte = USART_ReceiveData(USART1);  // ????????[3](@ref)
        
        switch (receive_state)
        {
            case STATE_WAIT_FF1:
                // ?????0xFF
                if (received_byte == 0xFF) {
                    receive_state = STATE_WAIT_FF2;  // ?????????????
                }
                // ????0xFF,??????,????????
                break;
                
            case STATE_WAIT_FF2:
                // ??????0xFF,?????????
                if (received_byte == 0xFF) {
                    receive_state = STATE_WAIT_DATA1;  // ?????1??
                } else if (received_byte == 0xFE) {
                    receive_state = STATE_WAIT_DATA2;  // ?????2??
                } else {
                    // ??????,?????[6](@ref)
                    receive_state = STATE_WAIT_FF1;
                }
                break;
                
            case STATE_WAIT_DATA1:
                // ???0xFF 0xFF,???????1
                LVGL_Angle_Value[0] = received_byte;
                receive_state = STATE_WAIT_FF1;  // ??????,?????
                break;
                
            case STATE_WAIT_DATA2:
                // ???0xFF 0xFE,???????2
                LVGL_Angle_Value[1] = received_byte;
                receive_state = STATE_WAIT_FF1;  // ??????,?????
								GET_DATA=1;
                break;
                
            default:
                // ????,?????
                receive_state = STATE_WAIT_FF1;
                break;
        }
        
        USART_ClearITPendingBit(USART1, USART_IT_RXNE);  // ????????[6](@ref)
    }
}
	

	

void USART1_IOinit(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;

	// ??GPIOA?USART1??? (USART1??APB2???)
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);

	// ??PA9?USART1_TX:??????
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	// ??PA10?USART1_RX:????
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	// ??USART1??:???115200,8???,???,1????
	USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // ?????????
	USART_Init(USART1, &USART_InitStructure);

	// ??USART1
	USART_Cmd(USART1, ENABLE);
	receive_state = STATE_WAIT_FF1;
}

void USART1_NVIC_init(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;

	// ??USART1?????NVIC
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);	
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // ?????
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5;         // ????
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	// ??USART1?????????? (RXNE)
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}

void USART1_INIT(void)
{
	USART1_IOinit();
	USART1_NVIC_init();
}
